#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: depends_demo.py
# @time: 2021/5/18 17:58
# @desc:
from typing import Optional

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()


oauth_scheme = OAuth2PasswordBearer(tokenUrl="token")


class QueryModel(BaseModel):
    q: Optional[str] = None
    skip: int = 0
    limit: int = 100


async def common_parameters(queryModel: QueryModel):
    return {"q": queryModel.q, "skip": queryModel.skip, "limit": queryModel.limit}


@app.post("/items/")
async def read_items(token: str = Depends(oauth_scheme)):
    return {"token": token}


@app.post("/users/")
async def read_users(commons: QueryModel = Depends(QueryModel)):
    return commons


if __name__ == '__main__':
    import os
    import uvicorn

    module = os.path.basename(__file__).split(".")[0]
    print(module)
    uvicorn.run(f'{module}:app', host="0.0.0.0", port=8000, reload=True, debug=True)
